import 'package:flutter/material.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/my_underline_tab_indicator.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/common/store_order_good_item.dart';
import 'package:xinxuan/controllers/store_order_controller/store_order_page_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/vo/store_order_list_vo.dart';

class StoreOrderPage extends StatelessWidget {

  // final StoreOrderPageController storeOrderPageController = Get.put(StoreOrderPageController());

  @override
  Widget build(BuildContext context) {
    // Map<String, dynamic> argumentsMap = Get.arguments;
    // int tabIndex = argumentsMap['tabIndex'];
    return GetBuilder<StoreOrderPageController>(
      init: StoreOrderPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: '订单列表',
            backgroundColor: pageBgColor,
          ),
          body: controller.data.value.meta!.aggregate!.length <= 0 ? Container() : DefaultTabController(
            length: controller.data.value.meta!.aggregate!.length, 
            initialIndex: controller.tabIndex.value,
            child: Column(
              children: [
                Container(
                  constraints: BoxConstraints(maxHeight: 44.h),
                  child: Material(
                    color: bgWhiteColor,
                    child: ScrollConfigurationCommon(child: TabBar(                
                      isScrollable: true,
                      labelPadding: EdgeInsets.zero,
                      indicator: MyUnderlineTabIndicator(
                        wantWidth: 20.w,
                        borderSide: BorderSide(
                          width: 5.h,
                          color: bgMainColor
                        )
                      ),
                      indicatorColor: bgMainColor,
                      labelColor: mainTextColor,
                      unselectedLabelColor: textSubtitleColor,
                      labelStyle: TextStyle(
                        fontSize: 14.sp
                      ),
                      unselectedLabelStyle: TextStyle(
                        fontSize: 14.sp
                      ),
                      onTap: (index) {
                        controller.tabIndex.value = index;
                        // controller.checkPickType();
                        controller.getStoreOrderList();
                      },
                      // tabs: controller.dataArr[controller.tabIndex.value].meta!.aggregate!.map((item) {
                      //    return _tabItem(item);
                      // }).toList()
                      tabs: controller.data.value.meta!.aggregate!.map((item) {
                         return _tabItem(item);
                      }).toList()
                    )),
                  ),
                ),
                // Expanded(child: Column(
                //   children: [
                //     SizedBox(height: 12.h),
                //     Expanded(child: TabBarView(
                //       physics: NeverScrollableScrollPhysics(),
                //       children: controller.dataArr.map((item) {
                //         return StoreOrderGoodItem(storeOrderListVo: item, storeOrderPageController: controller);
                //       }).toList(),
                //     ))
                //   ],
                // ))
                Expanded(child: Column(
                  children: [
                    SizedBox(height: 12.h),
                    Expanded(
                      // child: Obx(() => StoreOrderGoodItem(storeOrderListVo: controller.dataArr[controller.tabIndex.value], storeOrderPageController: controller))
                      child: Obx(() => StoreOrderGoodItem(storeOrderListVo: controller.data.value, storeOrderPageController: controller))
                    )
                  ],
                ))
              ],
            )
          ),
        );
      },
    );
  }

  Widget _tabItem(Aggregate item) {
    return Stack(
      children: [
        Container(
          width: 75.w,
          height: 50.h,
          alignment: Alignment.center,
          child: Text(
            '${item.label}'
          ),
        ),
        Positioned(
          right: 2.w,
          top: 8.h,
          child: item.total!<= 0 ? Container() : Container(
            padding: EdgeInsets.symmetric(horizontal: 3.w, vertical: 1.h),
            decoration: BoxDecoration(
              color: mainTextColor,
              borderRadius: BorderRadius.circular(8.h)
            ),
            child: Text(
              item.total! > 999 ? '999+' : '${item.total}',
              style: TextStyle(
                color: textWhiteColor,
                fontSize: 10.sp
              ),
            ),
          )
        )
      ],
    );
  }
}